<?php
/**
 * 系统设置 - JBlog
 *
 * @copyright (c) 2008-2012 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: Setting.php 30 2012-10-15 06:08:28Z lisijie86@gmail.com $
 */
class SettingController extends BaseController {
	
	
	public function indexAction() {
		$list = $this->db->getList("SELECT * FROM ".$this->db->getTable('config'));
		foreach ($list as $row) {
			${$row['group']}[$row['name']] = $this->makeConfigHtml($row);
		}
		
		if ( !isset($base['cachemode']) ) {
			$base['cachemode'] = array();
		} else {
			$base['cachemode'] = explode('|',$base['cachemode']);
		}
		$temp = str_replace(array('-','.'), '', $base['timezone']);
		$base['timezone'] < 0 ? ${'tz_0'.$temp} = 'selected' : ${'tz_'.$temp} = 'selected';
		
		$this->display();
	}
	
	public function saveAction() {
		initGP(array('addvar'), 'P', 'array');
		$config = array(
			'base' => (array)$_POST['base'],
			'seo' => (array)$_POST['seo'],
			'display' => (array)$_POST['display'],
			'upload' => (array)$_POST['upload'],
			'user' => (array)$_POST['user'],
			'myvar' => (array)$_POST['myvar'],
			'mail' => (array)$_POST['mail'],
		);
		//添加自定义变量
		if ( !empty($addvar['name']) ) {
			if ( !preg_match('/^[a-z_]+$/i', $addvar['name']) ) {
				prompt(__('自定义变量名 <b>%s</b> 不合法，必须由英文字母和下划线组成。',$addvar['name']));
			} elseif ( $db->fetch_one_array("SELECT * FROM ".tname('config')." WHERE `type` = 'myvar' AND `name` = '$addvar[name]'") ) {
				prompt(__('自定义变量名 <b>%s</b> 已存在。',$addvar['name']));
			}
			$db->insert('config', array('name'=>$addvar['name'], 'value'=>$addvar['value'], 'type'=>'myvar'));
		}
	
		if ( is_array($config['base']['cachemode']) ) {
			$cachemode = array();
			foreach ($config['base']['cachemode'] as $val) {
				if ( in_array($val, array('index','cate','post')) ) {
					$cachemode[] = $val;
				}
			}
			$config['base']['cachemode'] = implode('|',$cachemode);
		} else {
			$config['base']['cachemode'] = '';
		}
		if ( empty($config['base']['blogurl']) ) {
			$config['base']['blogurl'] = 'http://{host}'.substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/'));
		} else {
			if ( substr($config['base']['blogurl'],0,7) != 'http://' ) {
				$config['base']['blogurl'] = 'http://'.$config['base']['blogurl'];
			}
			$config['base']['blogurl'] = rtrim($config['base']['blogurl'],'/');
		}
		if ( empty($config['display']['pagesize']) ) {
			$config['display']['pagesize'] = 10;
		}
		if ( empty($config['display']['sidebar_num']) ) {
			$config['display']['sidebar_num'] = 10;
		}
		if ($config['seo']['urlmode'] == 1) {
			$base_root = str_replace('admin.php','',$_SERVER['PHP_SELF']);
			$content = "<IfModule mod_rewrite.c>\n"
					 . "\tRewriteEngine on\n"
					 . "\tRewriteBase $base_root\n"
					 . "\tRewriteCond %{REQUEST_FILENAME} !-f\n"
					 . "\tRewriteCond %{REQUEST_FILENAME} !-d\n"
					 . "\tRewriteRule ^.*$ index.php?$0 [L]\n"
					 . "</IfModule>";
			file_put_contents(JBLOG_ROOT.'.htaccess',$content);
		} else {
			@unlink(JBLOG_ROOT.'.htaccess');
		}
		if ( empty($config['display']['timeformat']) ) {
			$config['display']['timeformat'] = 'Y-m-d H:i';
		}
		$config['display']['comment_limit'] = abs(intval($config['display']['comment_limit']));
		if ( !$config['display']['comment_limit'] ) {
			$config['display']['comment_limit'] = 0;
		}
		$config['display']['reverse_order'] = $config['display']['reverse_order'] ? 1 : 0;
		$config['display']['auto_excerpt'] = $config['display']['auto_excerpt'] ? 1 : 0;
		$config['display']['link_tag'] = $config['display']['link_tag'] ? 1 : 0;
		foreach ($config as $type => $vars) {
			foreach ($vars as $key => $val) {
				$key = check_str($key);
				$val = $val;
				if ($key == 'close_reason') $val = nl2br($val);
				$db->insert('config', array('name' => $key, 'value'=>$val, 'type'=>$type), true);
			}
		}
		$config['mail']['mailsendtype'] = $config['mail']['mailsendtype'] ? 1 : 0;
		$config['mail']['reply_mail'] = $config['mail']['reply_mail'] ? 1 : 0;
		$config['mail']['reply_mail_type'] = (int)$config['mail']['mailsendtype'];
		do_action('config_save');
		recache('config,mail');
		prompt(__('成功更改配置信息，你可能需要刷新一下缓存！'),'admin.php?mod=config');		
	}
	
	private function makeConfigHtml($config) {
		
	}
}

